(function(){
    _$(document).ready(function(){
        function App(){};
        var page=0;
        var key=true;
        var key1=true;
        var page1=0;
        App.prototype.ajaxFn=function(){
            _$.post('/getbookInfo',{bid:bid},function(data){
                if(data['code']==1){
                    var info=data['info']['bookInfo'];
                    var ainfo=data['info']['ainfo'];
                    var brinfo=data['info']['brinfo'];
                    var brimg=data['info']['brni']['brimg'];
                    var brnick=data['info']['brni']['brnick'];
                    for(var i=0;i<brinfo.length;i++){
                        brinfo[i].imgurl=brimg[i];
                        brinfo[i].nickname=brnick[i];
                    }
                    if(info!=""||info!=null||info!=undefined){
                        _$("#bookMonth").html(info['month']);
                        _$("#bookrecomm").html(info['recomm']);
                        _$("#reward").html(info['reward']);
                    }
                }
            });
            //全部评论
            _$.ajax({
                type: 'post',
                url: '/getforumCount',
                data: {bid: bid},
                async: false,
                success: function (json) {
                    var dropload = $('#bookCommnetLC').dropload({
                        scrollArea: window,
                        domDown: {
                            domClass: 'dropload-down',
                            domRefresh: '<div class="dropload-refresh">↑上拉加载</div>',
                            domLoad: '<div class="dropload-load"><span class="loading"></span>加载中...</div>',
                            domNoData: '<div class="dropload-noData">已显示全部数据</div>'
                        },
                        //上拉
                        loadDownFn: function (me) {
                            var pageLength = json['count'];
                            console.log(pageLength);
                            if (json['count'] % 20 > 0) {
                                pageLength = (json['count'] / 20) + 1;
                            }
                            if (page > pageLength) {
                                me.lock();
                                me.noData();
                                me.resetload();
                            } else {
                                console.log(page);
                                _$.ajax({
                                    type: 'post',
                                    url: '/getforumList',
                                    data: {page: page, bid: bid,key:key},
                                    async: false,
                                    success: function (data) {
                                        console.log(data);
                                        if (data.code == 1) {
                                            key=false;
                                            var info = data.info.data;
                                            var brimg=data.info.userInfo.userimg;
                                            var brnick=data.info.userInfo.usernick;
                                            if (info == "") {
                                                me.lock();
                                                me.noData();
                                                me.resetload();
                                            } else {
                                                for(var i=0;i<info.length;i++){
                                                    info[i].imgurl=brimg[i];
                                                    info[i].nickname=brnick[i];
                                                }
                                                if (info.length > 0) {
                                                    var html="";
                                                    for(var val in info){
                                                        info[val]['startTime']=getTimes(info[val]['startTime']);
                                                        html += template('reviewtpl', info[val]);
                                                    }
                                                    // 为了测试，延迟1秒加载
                                                    setTimeout(function () {
                                                        // 插入加载使用 html() 重置 DOM
                                                        _$("#bookCommnetLC .dropload-down").before(html);
                                                        listClick();
                                                        page++;
                                                        // 每次数据加载完，必须重置
                                                        me.resetload();
                                                    }, 1000);
                                                }
                                            }
                                        }else if(data.code==2){
                                            _$("#bookCommnetLC").html('<div class="null-x"><div class="null">无书评记录</div></div>');
                                            // 每次数据加载完，必须重置
                                            me.lock();
                                            me.noData();
                                            me.resetload();
                                        }else {
                                            me.lock();
                                            me.noData();
                                            me.resetload();
                                        }
                                    }
                                });
                            }
                        }
                    });
                }
            });
            //热门评论
            _$.ajax({
                type: 'post',
                url: '/getforumHot',
                data: {bid: bid},
                async: false,
                success: function (json) {
                    var dropload = $('#bookCommnetL').dropload({
                        scrollArea: window,
                        domDown: {
                            domClass: 'dropload-down',
                            domRefresh: '<div class="dropload-refresh">↑上拉加载</div>',
                            domLoad: '<div class="dropload-load"><span class="loading"></span>加载中...</div>',
                            domNoData: '<div class="dropload-noData">已显示全部数据</div>'
                        },
                        //上拉
                        loadDownFn: function (me1) {
                            var pageLength1 = json['count'];
                            console.log(pageLength1);
                            if (json['count'] % 10 > 0) {
                                pageLength1 = (json['count'] / 10) + 1;
                            }
                            if (page1 > pageLength1) {
                                me1.lock();
                                me1.noData();
                                me1.resetload();
                            } else {
                                console.log(page1);
                                _$.ajax({
                                    type: 'post',
                                    url: '/getforumHotList',
                                    data: {page: page1, bid: bid,key:key1},
                                    async: false,
                                    success: function (data) {
                                        console.log(data);
                                        if (data.code == 1) {
                                            key1=false;
                                            var info = data.info.data;
                                            var brimg=data.info.userInfo.userimg;
                                            var brnick=data.info.userInfo.usernick;
                                            if (info == "") {
                                                me1.lock();
                                                me1.noData();
                                                me1.resetload();
                                            } else {
                                                for(var i=0;i<info.length;i++){
                                                    info[i].imgurl=brimg[i];
                                                    info[i].nickname=brnick[i];
                                                }
                                                if (info.length > 0) {
                                                    var html="";
                                                    for(var val in info){
                                                        info[val]['startTime']=getTimes(info[val]['startTime']);
                                                        html += template('reviewtpl', info[val]);
                                                    }
                                                    // 为了测试，延迟1秒加载
                                                    setTimeout(function () {
                                                        // 插入加载使用 html() 重置 DOM
                                                        _$("#bookCommnetL .dropload-down").before(html);
                                                        listClick();
                                                        page1++;
                                                        // 每次数据加载完，必须重置
                                                        me1.resetload();
                                                    }, 1000);
                                                }
                                            }
                                        }else if(data.code==2){
                                            _$("#bookCommnetL").html('<div class="null-x"><div class="null">无书评记录</div></div>');
                                            // 每次数据加载完，必须重置
                                            me1.noData();
                                        }else {
                                            me1.lock();
                                            me1.noData();
                                            me1.resetload();
                                        }
                                    }
                                });
                            }
                        }
                    })
                }
            });
            function listClick(){
                _$(".book-li").click(function(){
                    location.href="/book/thread/"+bid+"?brid="+_$(this).attr('data-thread-id');
                });
            }
        };
        App.prototype.eventFn=function(){
            _$(".module nav .acc-tab").click(function(){
                var index=_$(this).index();
                _$(this).addClass('active').siblings('.acc-tab').removeClass('active');
                _$(".account-msg .list").eq(index).addClass('active').siblings('.list').removeClass('active');
            });
        };
        App.prototype.init=function(){
            //事件调用
            this.ajaxFn();
            this.eventFn();
        };
        //程序初始化
        new App().init();
    });
})();